package com.moive.news.tqnewsapplication.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.BaseFragment;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.ui.activity.CollectActivity;
import com.moive.news.tqnewsapplication.ui.activity.FansActivity;
import com.moive.news.tqnewsapplication.ui.activity.HistoricalActivity;
import com.moive.news.tqnewsapplication.ui.activity.LoginActivity;
import com.moive.news.tqnewsapplication.ui.activity.NewsHistorActivity;
import com.moive.news.tqnewsapplication.ui.activity.PersonActivity;
import com.moive.news.tqnewsapplication.ui.activity.ProfileSwipeActivity;
import com.moive.news.tqnewsapplication.ui.activity.SettingActivity;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.moive.news.tqnewsapplication.weight.HeaderZoomLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/7.
 */

public class MeFragment extends BaseFragment {
    @BindView(R.id.ivBg)
    ImageView ivBg;
    @BindView(R.id.img_avatar)
    CircleImageView imgAvatar;
    @BindView(R.id.txt_user_name)
    TextView txtUserName;
    @BindView(R.id.img_toutiaohao)
    ImageView imgToutiaohao;
    @BindView(R.id.img_header_click_tips)
    ImageView imgHeaderClickTips;
    @BindView(R.id.layout_user_name)
    LinearLayout layoutUserName;
    @BindView(R.id.txt_following_count)
    TextView txtFollowingCount;
    @BindView(R.id.txt_following_title)
    TextView txtFollowingTitle;
    @BindView(R.id.layout_following)
    LinearLayout layoutFollowing;
    @BindView(R.id.v_user_info_divider_one)
    View vUserInfoDividerOne;
    @BindView(R.id.txt_followers_count)
    TextView txtFollowersCount;
    @BindView(R.id.txt_followers_title)
    TextView txtFollowersTitle;
    @BindView(R.id.layout_followers)
    LinearLayout layoutFollowers;
    @BindView(R.id.v_user_info_divider_two)
    View vUserInfoDividerTwo;
    @BindView(R.id.txt_recent_visitors_count)
    TextView txtRecentVisitorsCount;
    @BindView(R.id.txt_recent_visitors_title)
    TextView txtRecentVisitorsTitle;
    @BindView(R.id.layout_recent_visitors)
    LinearLayout layoutRecentVisitors;
    @BindView(R.id.layout_count)
    LinearLayout layoutCount;
    @BindView(R.id.layout_user_info)
    LinearLayout layoutUserInfo;
    @BindView(R.id.relat_01_img)
    ImageView relat01Img;
    @BindView(R.id.relat_01)
    RelativeLayout relat01;
    @BindView(R.id.relat_02_img)
    ImageView relat02Img;
    @BindView(R.id.relat_02)
    RelativeLayout relat02;
    @BindView(R.id.relat_03_img)
    ImageView relat03Img;
    @BindView(R.id.relat_03)
    RelativeLayout relat03;
    @BindView(R.id.relat_04_img)
    ImageView relat04Img;
    @BindView(R.id.relat_04)
    RelativeLayout relat04;
    @BindView(R.id.zommLayout)
    HeaderZoomLayout zommLayout;
    Unbinder unbinder;
    @BindView(R.id.mine_unlogin)
    TextView mineUnlogin;
    private View view;
    private static final String TAG = "MeFragment";
  private String all="";
    String token = "";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me, null);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }
    private void init() {
        txtUserName.setText(PreferenceUtil.getString("nickName", ""));
        token = PreferenceUtil.getString("token", "");
        if (TextUtils.isEmpty(token)) {
            mineUnlogin.setVisibility(View.VISIBLE);
        } else {
            mineUnlogin.setVisibility(View.GONE);
        }
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden==false){
            init();
            String img = PreferenceUtil.getString("headImg", "");
            Glide.with(getActivity())
                    .load(img)
                    .centerCrop()
                    .dontAnimate()
                    .placeholder(R.drawable.icon)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imgAvatar);
            txtUserName.setText(PreferenceUtil.getString("nickName", ""));
            token = PreferenceUtil.getString("token", "");
            if (TextUtils.isEmpty(token)) {
            } else {
                search();
            }
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.img_avatar, R.id.relat_01, R.id.layout_followers, R.id.layout_recent_visitors,
            R.id.relat_02, R.id.relat_03, R.id.relat_04, R.id.layout_following, R.id.mine_unlogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_avatar:
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法使用此功能！");
                }else {
                    startActivity(new Intent(getActivity(), PersonActivity.class));
                }
                break;
            case R.id.relat_01:
//                ToastUtils.ShortToast("点1");
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法使用此功能！");
                }else {
                    startActivity(new Intent(getActivity(), HistoricalActivity.class));
                }
                break;
            case R.id.relat_02:
                if (TextUtils.isEmpty(token)) {
                    ToastUtils.ShortToast("你还未登录，无法使用此功能！");
                }else {
                    startActivity(new Intent(getActivity(), CollectActivity.class));
                }

                break;
            case R.id.relat_03:
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法使用此功能！");
                }else {
                    startActivity(new Intent(getActivity(), NewsHistorActivity.class));
                }

                break;
            case R.id.relat_04:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            //动态
            case R.id.layout_following:
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法使用此功能！");
                }else {
                    startActivity(new Intent(getActivity(), ProfileSwipeActivity.class));
                }
                break;
            //关注
            case R.id.layout_followers:
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法使用此功能！");
                }else {
                    startActivity(new Intent(getActivity(), FansActivity.class));
                }
                break;
            //粉丝
            case R.id.layout_recent_visitors:
                if (TextUtils.isEmpty(PreferenceUtil.getString("token", ""))) {
                    ToastUtils.ShortToast("你还未登录，无法使用此功能！");
                }else {
                    startActivity(new Intent(getActivity(), FansActivity.class));
                }
                break;
            case R.id.mine_unlogin:
                Intent intent=new Intent(getActivity(),LoginActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("from","2");
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    private void search() {
        String userid = "";
        userid = PreferenceUtil.getString("id", "");
        if (TextUtils.isEmpty(userid)){
            return;
        }
        OkHttpUtils.post()
                .url(BaseApi.SRARCH_FANS)
                .addParams("userID", userid)
                .build().execute(new OkManagerCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }
            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.e(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                   if (jsonObject.optInt("code")==0){
                       JSONObject jsonObject1=jsonObject.getJSONObject("data");
                       String emotions=jsonObject1.getString("emotions");
                       String follow=jsonObject1.getString("follow");
                       String followed=jsonObject1.getString("followed");
                       txtFollowingCount.setText(emotions);
                       txtFollowersCount.setText(follow);
                       txtRecentVisitorsCount.setText(followed);
                   }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
